
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Stream · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="异步编程之Isolate.html" />
    
    
    <link rel="prev" href="异步编程之Future.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Stream</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x5F02;&#x6B65;&#x4E4B;-stream-&#x8BE6;&#x89E3;"><b></b>&#x5F02;&#x6B65;&#x4E4B; Stream &#x8BE6;&#x89E3;</a></li><ul><li><span class="title-icon "></span><a href="#&#x4EC0;&#x4E48;&#x662F;stream"><b></b>&#x4EC0;&#x4E48;&#x662F;Stream</a></li><li><span class="title-icon "></span><a href="#&#x5355;&#x8BA2;&#x9605;&#x6D41;"><b></b>&#x5355;&#x8BA2;&#x9605;&#x6D41;</a></li><ul><li><span class="title-icon "></span><a href="#&#x521B;&#x5EFA;-stream"><b></b>&#x521B;&#x5EFA; Stream</a></li><li><span class="title-icon "></span><a href="#&#x76D1;&#x542C;-stream"><b></b>&#x76D1;&#x542C; Stream</a></li><li><span class="title-icon "></span><a href="#stream-&#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;"><b></b>Stream &#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;</a></li><li><span class="title-icon "></span><a href="#streamcontroller"><b></b>StreamController</a></li></ul><li><span class="title-icon "></span><a href="#&#x5E7F;&#x64AD;&#x6D41;"><b></b>&#x5E7F;&#x64AD;&#x6D41;</a></li><li><span class="title-icon "></span><a href="#streamtransformer"><b></b>StreamTransformer</a></li><li><span class="title-icon "></span><a href="#&#x603B;&#x7ED3;"><b></b>&#x603B;&#x7ED3;</a></li></ul></ul></div><a href="#&#x5F02;&#x6B65;&#x4E4B;-stream-&#x8BE6;&#x89E3;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h1 id="&#x5F02;&#x6B65;&#x4E4B;-stream-&#x8BE6;&#x89E3;"><a name="&#x5F02;&#x6B65;&#x4E4B;-stream-&#x8BE6;&#x89E3;" class="anchor-navigation-ex-anchor" href="#&#x5F02;&#x6B65;&#x4E4B;-stream-&#x8BE6;&#x89E3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5F02;&#x6B65;&#x4E4B; Stream &#x8BE6;&#x89E3;</h1>
<p>&#x5173;&#x4E8E;Dart &#x8BED;&#x8A00;&#x7684;Stream &#x90E8;&#x5206;&#xFF0C;&#x5E94;&#x8BE5;&#x56DE;&#x5230;&#x8BED;&#x8A00;&#x672C;&#x8EAB;&#x53BB;&#x5BFB;&#x627E;&#x7B54;&#x6848;&#xFF0C;&#x8BB8;&#x591A;&#x8D44;&#x6599;&#x5728;Flutter&#x6846;&#x67B6;&#x4E2D;&#x56EB;&#x56F5;&#x541E;&#x67A3;&#x5F0F;&#x7684;&#x89E3;&#x91CA;<code>Stream</code>&#xFF0C;&#x603B;&#x6709;&#x4E00;&#x79CD;&#x8BA9;&#x4EBA;&#x4E91;&#x5C71;&#x96FE;&#x7F69;&#x7684;&#x611F;&#x89C9;&#xFF0C;&#x4E8B;&#x5B9E;&#x4E0A;&#x4ECE;Dart&#x8BED;&#x8A00;&#x672C;&#x8EAB;&#x53BB;&#x4E86;&#x89E3;Stream&#x5E76;&#x4E0D;&#x590D;&#x6742;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x5C31;&#x82B1;&#x70B9;&#x65F6;&#x95F4;&#x597D;&#x597D;&#x5B66;&#x4E60;&#x4E00;&#x4E0B;<code>Stream</code>&#x5427;&#xFF01;</p>
<p><code>Stream</code>&#x548C; <code>Future</code>&#x90FD;&#x662F;Dart&#x4E2D;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;&#x7684;&#x6838;&#x5FC3;&#x5185;&#x5BB9;&#xFF0C;&#x5728;&#x4E4B;&#x524D;&#x7684;&#x6587;&#x7AE0;&#x4E2D;&#x5DF2;&#x7ECF;&#x8BE6;&#x7EC6;&#x53D9;&#x8FF0;&#x4E86;&#x5173;&#x4E8E;<code>Future</code>&#x7684;&#x77E5;&#x8BC6;&#xFF0C;&#x8BF7;&#x67E5;&#x770B; <a href="https://blog.csdn.net/yingshukun/article/details/89840009" target="_blank">Dart &#x5F02;&#x6B65;&#x7F16;&#x7A0B;&#x8BE6;&#x89E3;</a>&#xFF0C;&#x672C;&#x7BC7;&#x6587;&#x7AE0;&#x5219;&#x4E3B;&#x8981;&#x57FA;&#x4E8E; Dart2.5 &#x4ECB;&#x7ECD;<code>Stream</code>&#x7684;&#x77E5;&#x8BC6;&#x3002;</p>
<h2 id="&#x4EC0;&#x4E48;&#x662F;stream"><a name="&#x4EC0;&#x4E48;&#x662F;stream" class="anchor-navigation-ex-anchor" href="#&#x4EC0;&#x4E48;&#x662F;stream"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4EC0;&#x4E48;&#x662F;Stream</h2>
<p><code>Stream</code>&#x662F;Dart&#x8BED;&#x8A00;&#x4E2D;&#x7684;&#x6240;&#x8C13;&#x5F02;&#x6B65;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x7684;&#x4E1C;&#x897F;&#xFF0C;&#x7B80;&#x5355;&#x7406;&#x89E3;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5F02;&#x6B65;&#x6570;&#x636E;&#x961F;&#x5217;&#x800C;&#x5DF2;&#x3002;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x961F;&#x5217;&#x7684;&#x7279;&#x70B9;&#x662F;&#x5148;&#x8FDB;&#x5148;&#x51FA;&#x7684;&#xFF0C;<code>Stream</code>&#x4E5F;&#x6B63;&#x662F;&#x5982;&#x6B64;
<img src="https://img-blog.csdnimg.cn/20190915152423489.gif" alt="&#x5728;&#x8FD9;&#x91CC;&#x63D2;&#x5165;&#x56FE;&#x7247;&#x63CF;&#x8FF0;">
&#x66F4;&#x5F62;&#x8C61;&#x7684;&#x6BD4;&#x55BB;&#xFF0C;<code>Stream</code>&#x5C31;&#x50CF;&#x4E00;&#x4E2A;&#x4F20;&#x9001;&#x5E26;&#x3002;&#x53EF;&#x4EE5;&#x5C06;&#x4E00;&#x4FA7;&#x7684;&#x7269;&#x54C1;&#x81EA;&#x52A8;&#x8FD0;&#x9001;&#x5230;&#x53E6;&#x4E00;&#x4FA7;&#x3002;&#x5982;&#x4E0A;&#x56FE;&#xFF0C;&#x5728;&#x53E6;&#x4E00;&#x4FA7;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4EBA;&#x53BB;&#x6293;&#x53D6;&#xFF0C;&#x7269;&#x54C1;&#x5C31;&#x4F1A;&#x6389;&#x843D;&#x6D88;&#x5931;&#x3002;
<img src="https://img-blog.csdnimg.cn/20190915152851279.gif" alt="&#x5728;&#x8FD9;&#x91CC;&#x63D2;&#x5165;&#x56FE;&#x7247;&#x63CF;&#x8FF0;">
&#x4F46;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5728;&#x672B;&#x5C3E;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x76D1;&#x542C;&#xFF0C;&#x5F53;&#x7269;&#x54C1;&#x5230;&#x8FBE;&#x672B;&#x7AEF;&#x65F6;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x89E6;&#x53D1;&#x76F8;&#x5E94;&#x7684;&#x54CD;&#x5E94;&#x884C;&#x4E3A;&#x3002;</p>
<blockquote>
<p>&#x5728;Dart&#x8BED;&#x8A00;&#x4E2D;&#xFF0C;<code>Stream</code>&#x6709;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#xFF0C;&#x4E00;&#x79CD;&#x662F;&#x70B9;&#x5BF9;&#x70B9;&#x7684;&#x5355;&#x8BA2;&#x9605;&#x6D41;&#xFF08;Single-subscription&#xFF09;&#xFF0C;&#x53E6;&#x4E00;&#x79CD;&#x5219;&#x662F;&#x5E7F;&#x64AD;&#x6D41;&#x3002;</p>
</blockquote>
<h2 id="&#x5355;&#x8BA2;&#x9605;&#x6D41;"><a name="&#x5355;&#x8BA2;&#x9605;&#x6D41;" class="anchor-navigation-ex-anchor" href="#&#x5355;&#x8BA2;&#x9605;&#x6D41;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5355;&#x8BA2;&#x9605;&#x6D41;</h2>
<p>&#x5355;&#x8BA2;&#x9605;&#x6D41;&#x7684;&#x7279;&#x70B9;&#x662F;&#x53EA;&#x5141;&#x8BB8;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x76D1;&#x542C;&#x5668;&#xFF0C;&#x5373;&#x4F7F;&#x8BE5;&#x76D1;&#x542C;&#x5668;&#x88AB;&#x53D6;&#x6D88;&#x540E;&#xFF0C;&#x4E5F;&#x4E0D;&#x5141;&#x8BB8;&#x518D;&#x6B21;&#x6CE8;&#x518C;&#x76D1;&#x542C;&#x5668;&#x3002;</p>
<h3 id="&#x521B;&#x5EFA;-stream"><a name="&#x521B;&#x5EFA;-stream" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;-stream"><i class="fa fa-link" aria-hidden="true"></i></a>&#x521B;&#x5EFA; Stream</h3>
<p>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;<code>Stream</code>&#x6709;9&#x4E2A;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#xFF0C;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x662F;&#x6784;&#x9020;&#x5E7F;&#x64AD;&#x6D41;&#x7684;&#xFF0C;&#x8FD9;&#x91CC;&#x4E3B;&#x8981;&#x770B;&#x4E00;&#x4E0B;&#x5176;&#x4E2D;5&#x4E2A;&#x6784;&#x9020;&#x5355;&#x8BA2;&#x9605;&#x6D41;&#x7684;&#x65B9;&#x6CD5;</p>
<h4 id="periodic"><a name="periodic" class="anchor-navigation-ex-anchor" href="#periodic"><i class="fa fa-link" aria-hidden="true"></i></a>periodic</h4>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main(){
  test();
}

test() <span class="hljs-keyword">async</span>{
  <span class="hljs-comment">// &#x4F7F;&#x7528; periodic &#x521B;&#x5EFA;&#x6D41;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x95F4;&#x9694;&#x65F6;&#x95F4;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x56DE;&#x8C03;&#x51FD;&#x6570;</span>
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), callback);
  <span class="hljs-comment">// await for&#x5FAA;&#x73AF;&#x4ECE;&#x6D41;&#x4E2D;&#x8BFB;&#x53D6;</span>
  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i <span class="hljs-keyword">in</span> stream){
    <span class="hljs-built_in">print</span>(i);
  }
}

<span class="hljs-comment">// &#x53EF;&#x4EE5;&#x5728;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x5BF9;&#x503C;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x8FD9;&#x91CC;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x4E86;</span>
<span class="hljs-built_in">int</span> callback(<span class="hljs-built_in">int</span> value){
  <span class="hljs-keyword">return</span> value;
}
</code></pre>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>0
1
2
3
4
...
</code></pre><p>&#x8BE5;&#x65B9;&#x6CD5;&#x4ECE;&#x6574;&#x6570;0&#x5F00;&#x59CB;&#xFF0C;&#x5728;&#x6307;&#x5B9A;&#x7684;&#x95F4;&#x9694;&#x65F6;&#x95F4;&#x5185;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x81EA;&#x7136;&#x6570;&#x5217;&#xFF0C;&#x4EE5;&#x4E0A;&#x8BBE;&#x7F6E;&#x4E3A;&#x6BCF;&#x4E00;&#x79D2;&#x751F;&#x6210;&#x4E00;&#x6B21;&#xFF0C;<code>callback</code>&#x51FD;&#x6570;&#x7528;&#x4E8E;&#x5BF9;&#x751F;&#x6210;&#x7684;&#x6574;&#x6570;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x5904;&#x7406;&#x540E;&#x518D;&#x653E;&#x5165;<code>Stream</code>&#x4E2D;&#x3002;&#x8FD9;&#x91CC;&#x5E76;&#x672A;&#x5904;&#x7406;&#xFF0C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x4E86;&#x3002;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x4E2A;&#x6D41;&#x662F;&#x65E0;&#x9650;&#x7684;&#xFF0C;&#x5B83;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x7EA6;&#x675F;&#x6761;&#x4EF6;&#x4F7F;&#x4E4B;&#x505C;&#x6B62;&#x3002;&#x5728;&#x540E;&#x9762;&#x4F1A;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x7ED9;&#x6D41;&#x8BBE;&#x7F6E;&#x6761;&#x4EF6;&#x3002;</p>
<h4 id="fromfuture"><a name="fromfuture" class="anchor-navigation-ex-anchor" href="#fromfuture"><i class="fa fa-link" aria-hidden="true"></i></a>fromFuture</h4>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> main(){
  test();
}

test() <span class="hljs-keyword">async</span>{
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;test start&quot;</span>);
  Future&lt;<span class="hljs-built_in">String</span>&gt; fut = Future((){
      <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;async task&quot;</span>;
  });

  <span class="hljs-comment">// &#x4ECE;Future&#x521B;&#x5EFA;Stream</span>
  Stream&lt;<span class="hljs-built_in">String</span>&gt; stream = Stream&lt;<span class="hljs-built_in">String</span>&gt;.fromFuture(fut);
  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> s <span class="hljs-keyword">in</span> stream){
    <span class="hljs-built_in">print</span>(s);
  }
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;test end&quot;</span>);
}
</code></pre>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>test start
async task
test end
</code></pre><p>&#x8BE5;&#x65B9;&#x6CD5;&#x4ECE;&#x4E00;&#x4E2A;<code>Future</code>&#x521B;&#x5EFA;<code>Stream</code>&#xFF0C;&#x5F53;<code>Future</code>&#x6267;&#x884C;&#x5B8C;&#x6210;&#x65F6;&#xFF0C;&#x5C31;&#x4F1A;&#x653E;&#x5165;<code>Stream</code>&#x4E2D;&#xFF0C;&#x800C;&#x540E;&#x4ECE;<code>Stream</code>&#x4E2D;&#x5C06;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&#x7684;&#x7ED3;&#x679C;&#x53D6;&#x51FA;&#x3002;&#x8FD9;&#x79CD;&#x7528;&#x6CD5;&#xFF0C;&#x5F88;&#x50CF;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;&#x961F;&#x5217;&#x3002;</p>
<h4 id="fromfutures"><a name="fromfutures" class="anchor-navigation-ex-anchor" href="#fromfutures"><i class="fa fa-link" aria-hidden="true"></i></a>fromFutures</h4>
<p>&#x4ECE;&#x591A;&#x4E2A;<code>Future</code>&#x521B;&#x5EFA;<code>Stream</code>&#xFF0C;&#x5373;&#x5C06;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;&#x653E;&#x5165;<code>Stream</code>&#x4E2D;&#xFF0C;&#x6BCF;&#x4E2A;<code>Future</code>&#x6309;&#x987A;&#x5E8F;&#x6267;&#x884C;&#xFF0C;&#x6267;&#x884C;&#x5B8C;&#x6210;&#x540E;&#x653E;&#x5165;<code>Stream</code></p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span>  <span class="hljs-string">&apos;dart:io&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  test();
}

test() <span class="hljs-keyword">async</span>{
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;test start&quot;</span>);
  Future&lt;<span class="hljs-built_in">String</span>&gt; fut1 = Future((){
      <span class="hljs-comment">// &#x6A21;&#x62DF;&#x8017;&#x65F6;5&#x79D2;</span>
      sleep(<span class="hljs-built_in">Duration</span>(seconds:<span class="hljs-number">5</span>));
      <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;async task1&quot;</span>;
  });
    Future&lt;<span class="hljs-built_in">String</span>&gt; fut2 = Future((){
      <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;async task2&quot;</span>;
  });

  <span class="hljs-comment">// &#x5C06;&#x591A;&#x4E2A;Future&#x653E;&#x5165;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x4E2D;&#xFF0C;&#x5C06;&#x8BE5;&#x5217;&#x8868;&#x4F20;&#x5165;</span>
  Stream&lt;<span class="hljs-built_in">String</span>&gt; stream = Stream&lt;<span class="hljs-built_in">String</span>&gt;.fromFutures([fut1,fut2]);
  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> s <span class="hljs-keyword">in</span> stream){
    <span class="hljs-built_in">print</span>(s);
  }
  <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;test end&quot;</span>);
}
</code></pre>
<h4 id="fromiterable"><a name="fromiterable" class="anchor-navigation-ex-anchor" href="#fromiterable"><i class="fa fa-link" aria-hidden="true"></i></a>fromIterable</h4>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x4ECE;&#x4E00;&#x4E2A;&#x96C6;&#x5408;&#x521B;&#x5EFA;<code>Stream</code>&#xFF0C;&#x7528;&#x6CD5;&#x4E0E;&#x4E0A;&#x9762;&#x4F8B;&#x5B50;&#x5927;&#x81F4;&#x76F8;&#x540C;</p>
<pre><code class="lang-dart"><span class="hljs-comment">// &#x4ECE;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x521B;&#x5EFA;`Stream`</span>
Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.fromIterable([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]);
</code></pre>
<h4 id="value"><a name="value" class="anchor-navigation-ex-anchor" href="#value"><i class="fa fa-link" aria-hidden="true"></i></a>value</h4>
<p>&#x8FD9;&#x662F;Dart2.5 &#x65B0;&#x589E;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x4E8E;&#x4ECE;&#x5355;&#x4E2A;&#x503C;&#x521B;&#x5EFA;<code>Stream</code></p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  Stream&lt;<span class="hljs-built_in">bool</span>&gt; stream = Stream&lt;<span class="hljs-built_in">bool</span>&gt;.value(<span class="hljs-keyword">false</span>);
  <span class="hljs-comment">// await for&#x5FAA;&#x73AF;&#x4ECE;&#x6D41;&#x4E2D;&#x8BFB;&#x53D6;</span>
  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i <span class="hljs-keyword">in</span> stream){
    <span class="hljs-built_in">print</span>(i);
  }
}
</code></pre>
<h3 id="&#x76D1;&#x542C;-stream"><a name="&#x76D1;&#x542C;-stream" class="anchor-navigation-ex-anchor" href="#&#x76D1;&#x542C;-stream"><i class="fa fa-link" aria-hidden="true"></i></a>&#x76D1;&#x542C; Stream</h3>
<p>&#x76D1;&#x542C;<code>Stream</code>&#xFF0C;&#x5E76;&#x4ECE;&#x4E2D;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x4E5F;&#x6709;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x4E00;&#x79CD;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x4E0A;&#x6587;&#x4E2D;&#x4F7F;&#x7528;&#x7684;<code>await for</code>&#x5FAA;&#x73AF;&#xFF0C;&#x8FD9;&#x4E5F;&#x662F;&#x5B98;&#x65B9;&#x63A8;&#x8350;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x770B;&#x8D77;&#x6765;&#x66F4;&#x7B80;&#x6D01;&#x53CB;&#x597D;&#xFF0C;&#x9664;&#x6B64;&#x4E4B;&#x5916;&#xFF0C;&#x53E6;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x5206;&#x522B;&#x662F;&#x4F7F;&#x7528;<code>forEach</code>&#x65B9;&#x6CD5;&#x6216;<code>listen</code>&#x65B9;&#x6CD5;</p>
<pre><code class="lang-dart">  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), callback);
  <span class="hljs-comment">// &#x4F7F;&#x7528;forEach&#xFF0C;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8FDB;&#x53BB;&#x83B7;&#x53D6;&#x5E76;&#x5904;&#x7406;&#x6570;&#x636E;</span>
  stream.forEach((<span class="hljs-built_in">int</span> x){
    <span class="hljs-built_in">print</span>(x);
  });
</code></pre>
<p>&#x4F7F;&#x7528; <code>listen</code> &#x76D1;&#x542C;
<code>StreamSubscription&lt;T&gt; listen(void onData(T event), {Function onError, void onDone(), bool cancelOnError})</code></p>
<pre><code class="lang-dart">  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), callback);
  stream.listen((x){
    <span class="hljs-built_in">print</span>(x);
  });
</code></pre>
<p>&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x51E0;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x53C2;&#x6570;</p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), callback);
  stream = stream.take(<span class="hljs-number">5</span>);
  stream.listen(
    (x)=&gt;<span class="hljs-built_in">print</span>(x),
  onError: (e)=&gt;<span class="hljs-built_in">print</span>(e),
  onDone: ()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;onDone&quot;</span>));
}
</code></pre>
<ul>
<li><code>onError</code>&#xFF1A;&#x53D1;&#x751F;Error&#x65F6;&#x89E6;&#x53D1;</li>
<li><code>onDone</code>&#xFF1A;&#x5B8C;&#x6210;&#x65F6;&#x89E6;&#x53D1;</li>
<li><code>unsubscribeOnError</code>&#xFF1A;&#x9047;&#x5230;&#x7B2C;&#x4E00;&#x4E2A;Error&#x65F6;&#x662F;&#x5426;&#x53D6;&#x6D88;&#x76D1;&#x542C;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;<code>false</code></li>
</ul>
<h3 id="stream-&#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;"><a name="stream-&#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#stream-&#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>Stream &#x7684;&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;</h3>
<h4 id="take-&#x548C;-takewhile"><a name="take-&#x548C;-takewhile" class="anchor-navigation-ex-anchor" href="#take-&#x548C;-takewhile"><i class="fa fa-link" aria-hidden="true"></i></a>take &#x548C; takeWhile</h4>
<p><code>Stream&lt;T&gt; take(int count)</code> &#x7528;&#x4E8E;&#x9650;&#x5236;<code>Stream</code>&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x6570;&#x91CF;</p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), callback);
  <span class="hljs-comment">// &#x5F53;&#x653E;&#x5165;&#x4E09;&#x4E2A;&#x5143;&#x7D20;&#x540E;&#xFF0C;&#x76D1;&#x542C;&#x4F1A;&#x505C;&#x6B62;&#xFF0C;Stream&#x4F1A;&#x5173;&#x95ED;</span>
  stream = stream.take(<span class="hljs-number">3</span>);

  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i <span class="hljs-keyword">in</span> stream){
    <span class="hljs-built_in">print</span>(i);
  }
}
</code></pre>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>0
1
2
</code></pre><p><code>Stream&lt;T&gt;.takeWhile(bool test(T element))</code> &#x4E0E; <code>take</code>&#x4F5C;&#x7528;&#x76F8;&#x4F3C;&#xFF0C;&#x53EA;&#x662F;&#x5B83;&#x7684;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7C7B;&#x578B;&#xFF0C;&#x4E14;&#x8FD4;&#x56DE;&#x503C;&#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;<code>bool</code>&#x503C;</p>
<pre><code class="lang-dart">  stream = stream.takeWhile((x){
    <span class="hljs-comment">// &#x5BF9;&#x5F53;&#x524D;&#x5143;&#x7D20;&#x8FDB;&#x884C;&#x5224;&#x65AD;&#xFF0C;&#x4E0D;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x5219;&#x53D6;&#x6D88;&#x76D1;&#x542C;</span>
    <span class="hljs-keyword">return</span> x &lt;= <span class="hljs-number">3</span>;
  });
</code></pre>
<h4 id="skip-&#x548C;-skipwhile"><a name="skip-&#x548C;-skipwhile" class="anchor-navigation-ex-anchor" href="#skip-&#x548C;-skipwhile"><i class="fa fa-link" aria-hidden="true"></i></a>skip &#x548C; skipWhile</h4>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), callback);
  stream = stream.take(<span class="hljs-number">5</span>);
  <span class="hljs-comment">// &#x8868;&#x793A;&#x4ECE;Stream&#x4E2D;&#x8DF3;&#x8FC7;&#x4E24;&#x4E2A;&#x5143;&#x7D20;</span>
  stream = stream.skip(<span class="hljs-number">2</span>);

  <span class="hljs-keyword">await</span> <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i <span class="hljs-keyword">in</span> stream){
    <span class="hljs-built_in">print</span>(i);
  }
}
</code></pre>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>2
3
4
</code></pre><p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x8BE5;&#x65B9;&#x6CD5;&#x53EA;&#x662F;&#x4ECE;<code>Stream</code>&#x4E2D;&#x83B7;&#x53D6;&#x5143;&#x7D20;&#x65F6;&#x8DF3;&#x8FC7;&#xFF0C;&#x88AB;&#x8DF3;&#x8FC7;&#x7684;&#x5143;&#x7D20;&#x4F9D;&#x7136;&#x662F;&#x88AB;&#x6267;&#x884C;&#x4E86;&#x7684;&#xFF0C;&#x6240;&#x8017;&#x8D39;&#x7684;&#x65F6;&#x95F4;&#x4F9D;&#x7136;&#x5B58;&#x5728;&#xFF0C;&#x5176;&#x5B9E;&#x53EA;&#x662F;&#x8DF3;&#x8FC7;&#x4E86;&#x6267;&#x884C;&#x5B8C;&#x7684;&#x7ED3;&#x679C;&#x800C;&#x5DF2;&#x3002;</p>
<p><code>Stream&lt;T&gt; skipWhile(bool test(T element))</code> &#x65B9;&#x6CD5;&#x4E0E;<code>takeWhile</code>&#x7528;&#x6CD5;&#x662F;&#x76F8;&#x540C;&#x7684;&#xFF0C;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5BF9;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x5224;&#x65AD;&#xFF0C;&#x8868;&#x793A;&#x8DF3;&#x8FC7;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#x7684;&#x3002;</p>
<h4 id="tolist"><a name="tolist" class="anchor-navigation-ex-anchor" href="#tolist"><i class="fa fa-link" aria-hidden="true"></i></a>toList</h4>
<p><code>Future&lt;List&lt;T&gt;&gt; toList()</code> &#x8868;&#x793A;&#x5C06;<code>Stream</code>&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x5728;List&#x4E2D;</p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), callback);
  stream = stream.take(<span class="hljs-number">5</span>);
  <span class="hljs-built_in">List</span> &lt;<span class="hljs-built_in">int</span>&gt; data = <span class="hljs-keyword">await</span> stream.toList(); 
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i <span class="hljs-keyword">in</span> data){ 
      <span class="hljs-built_in">print</span>(i);
   } 
}
</code></pre>
<h4 id="&#x5C5E;&#x6027;-length"><a name="&#x5C5E;&#x6027;-length" class="anchor-navigation-ex-anchor" href="#&#x5C5E;&#x6027;-length"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5C5E;&#x6027; length</h4>
<p>&#x7B49;&#x5F85;&#x5E76;&#x83B7;&#x53D6;&#x6D41;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#x7684;&#x6570;&#x91CF;</p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), callback);
  stream = stream.take(<span class="hljs-number">5</span>);
  <span class="hljs-keyword">var</span> len = <span class="hljs-keyword">await</span> stream.length;
  <span class="hljs-built_in">print</span>(len);
}
</code></pre>
<h3 id="streamcontroller"><a name="streamcontroller" class="anchor-navigation-ex-anchor" href="#streamcontroller"><i class="fa fa-link" aria-hidden="true"></i></a>StreamController</h3>
<p>&#x5B83;&#x5B9E;&#x9645;&#x4E0A;&#x5C31;&#x662F;<code>Stream</code>&#x7684;&#x4E00;&#x4E2A;&#x5E2E;&#x52A9;&#x7C7B;&#xFF0C;&#x53EF;&#x7528;&#x4E8E;&#x6574;&#x4E2A; <code>Stream</code> &#x8FC7;&#x7A0B;&#x7684;&#x63A7;&#x5236;&#x3002;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:async&apos;</span>;

<span class="hljs-keyword">void</span> main() {
  test();
}

test() <span class="hljs-keyword">async</span>{
  <span class="hljs-comment">// &#x521B;&#x5EFA;</span>
  StreamController streamController = StreamController();
  <span class="hljs-comment">// &#x653E;&#x5165;&#x4E8B;&#x4EF6;</span>
  streamController.add(<span class="hljs-string">&apos;element_1&apos;</span>);
  streamController.addError(<span class="hljs-string">&quot;this is error&quot;</span>);
  streamController.sink.add(<span class="hljs-string">&apos;element_2&apos;</span>);
  streamController.stream.listen(
    <span class="hljs-built_in">print</span>,
  onError: <span class="hljs-built_in">print</span>,
  onDone: ()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;onDone&quot;</span>));
}
</code></pre>
<p>&#x4F7F;&#x7528;&#x8BE5;&#x7C7B;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x5BFC;&#x5165;<code>&apos;dart:async&apos;</code>&#xFF0C;&#x5176;<code>add</code>&#x65B9;&#x6CD5;&#x548C;<code>sink.add</code>&#x65B9;&#x6CD5;&#x662F;&#x76F8;&#x540C;&#x7684;&#xFF0C;&#x90FD;&#x662F;&#x7528;&#x4E8E;&#x653E;&#x5165;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;<code>addError</code>&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;&#x76D1;&#x542C;&#x65B9;&#x6CD5;&#x4E2D;&#x7684;<code>onError</code>&#x53EF;&#x83B7;&#x53D6;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x8FD8;&#x53EF;&#x4EE5;&#x5728;<code>StreamController</code>&#x4E2D;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x6307;&#x5B9A;&#x7684;<code>stream</code></p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), (e)=&gt;e);
  stream = stream.take(<span class="hljs-number">5</span>);

  StreamController sc = StreamController();
  <span class="hljs-comment">// &#x5C06; Stream &#x4F20;&#x5165;</span>
  sc.addStream(stream);
  <span class="hljs-comment">// &#x76D1;&#x542C;</span>
  sc.stream.listen(
    <span class="hljs-built_in">print</span>,
  onDone: ()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;onDone&quot;</span>));
}
</code></pre>
<p>&#x73B0;&#x5728;&#x6765;&#x770B;&#x4E00;&#x4E0B;<code>StreamController</code>&#x7684;&#x539F;&#x578B;&#xFF0C;&#x5B83;&#x6709;5&#x4E2A;&#x53EF;&#x9009;&#x53C2;&#x6570;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">factory</span> StreamController(
      {<span class="hljs-keyword">void</span> onListen(),
      <span class="hljs-keyword">void</span> onPause(),
      <span class="hljs-keyword">void</span> onResume(),
      onCancel(),
      <span class="hljs-built_in">bool</span> <span class="hljs-keyword">sync</span>: <span class="hljs-keyword">false</span>})
</code></pre>
<ul>
<li><code>onListen</code> &#x6CE8;&#x518C;&#x76D1;&#x542C;&#x65F6;&#x56DE;&#x8C03;</li>
<li><code>onPause</code> &#x5F53;&#x6D41;&#x6682;&#x505C;&#x65F6;&#x56DE;&#x8C03;</li>
<li><code>onResume</code> &#x5F53;&#x6D41;&#x6062;&#x590D;&#x65F6;&#x56DE;&#x8C03;</li>
<li><code>onCancel</code> &#x5F53;&#x76D1;&#x542C;&#x5668;&#x88AB;&#x53D6;&#x6D88;&#x65F6;&#x56DE;&#x8C03;</li>
<li><code>sync</code> &#x5F53;&#x503C;&#x4E3A;<code>true</code>&#x65F6;&#x8868;&#x793A;&#x540C;&#x6B65;&#x63A7;&#x5236;&#x5668;<code>SynchronousStreamController</code>&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;<code>false</code>&#xFF0C;&#x8868;&#x793A;&#x5F02;&#x6B65;&#x63A7;&#x5236;&#x5668;</li>
</ul>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  <span class="hljs-comment">// &#x521B;&#x5EFA;</span>
  StreamController sc = StreamController(
    onListen: ()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;onListen&quot;</span>),
    onPause: ()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;onPause&quot;</span>),
    onResume: ()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;onResume&quot;</span>),
    onCancel: ()=&gt;<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;onCancel&quot;</span>),
    <span class="hljs-keyword">sync</span>:<span class="hljs-keyword">false</span>
  );

  StreamSubscription ss = sc.stream.listen(<span class="hljs-built_in">print</span>);

  sc.add(<span class="hljs-string">&apos;element_1&apos;</span>);

  <span class="hljs-comment">// &#x6682;&#x505C;</span>
  ss.pause();
  <span class="hljs-comment">// &#x6062;&#x590D;</span>
  ss.resume();
  <span class="hljs-comment">// &#x53D6;&#x6D88;</span>
  ss.cancel();

  <span class="hljs-comment">// &#x5173;&#x95ED;&#x6D41;</span>
  sc.close();
}
</code></pre>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>onListen
onPause
onCancel
</code></pre><p>&#x56E0;&#x4E3A;&#x76D1;&#x542C;&#x5668;&#x88AB;&#x53D6;&#x6D88;&#x4E86;&#xFF0C;&#x4E14;&#x5173;&#x95ED;&#x4E86;&#x6D41;&#xFF0C;&#x5BFC;&#x81F4;<code>&quot;element_1&quot;</code>&#x672A;&#x88AB;&#x8F93;&#x51FA;&#xFF0C;<code>&quot;onResume&quot;</code>&#x4EA6;&#x672A;&#x8F93;&#x51FA;</p>
<h2 id="&#x5E7F;&#x64AD;&#x6D41;"><a name="&#x5E7F;&#x64AD;&#x6D41;" class="anchor-navigation-ex-anchor" href="#&#x5E7F;&#x64AD;&#x6D41;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5E7F;&#x64AD;&#x6D41;</h2>
<p>&#x5982;&#x4E0B;&#xFF0C;&#x5728;&#x666E;&#x901A;&#x7684;&#x5355;&#x8BA2;&#x9605;&#x6D41;&#x4E2D;&#x8C03;&#x7528;&#x4E24;&#x6B21;<code>listen</code>&#x4F1A;&#x62A5;&#x9519;</p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), (e)=&gt;e);
  stream = stream.take(<span class="hljs-number">5</span>);

  stream.listen(<span class="hljs-built_in">print</span>);
  stream.listen(<span class="hljs-built_in">print</span>);
}
</code></pre>
<pre><code>Unhandled exception:
Bad state: Stream has already been listened to.
</code></pre><p>&#x524D;&#x9762;&#x5DF2;&#x7ECF;&#x8BF4;&#x4E86;&#x5355;&#x8BA2;&#x9605;&#x6D41;&#x7684;&#x7279;&#x70B9;&#xFF0C;&#x800C;&#x5E7F;&#x64AD;&#x6D41;&#x5219;&#x53EF;&#x4EE5;&#x5141;&#x8BB8;&#x591A;&#x4E2A;&#x76D1;&#x542C;&#x5668;&#x5B58;&#x5728;&#xFF0C;&#x5C31;&#x5982;&#x540C;&#x5E7F;&#x64AD;&#x4E00;&#x6837;&#xFF0C;&#x51E1;&#x662F;&#x76D1;&#x542C;&#x4E86;&#x5E7F;&#x64AD;&#x6D41;&#xFF0C;&#x6BCF;&#x4E2A;&#x76D1;&#x542C;&#x5668;&#x90FD;&#x80FD;&#x83B7;&#x53D6;&#x5230;&#x6570;&#x636E;&#x3002;&#x8981;&#x6CE8;&#x610F;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x89E6;&#x53D1;&#x4E8B;&#x4EF6;&#x65F6;&#x5C06;&#x76D1;&#x542C;&#x8005;&#x6B63;&#x6DFB;&#x52A0;&#x5230;&#x5E7F;&#x64AD;&#x6D41;&#xFF0C;&#x5219;&#x8BE5;&#x76D1;&#x542C;&#x5668;&#x5C06;&#x4E0D;&#x4F1A;&#x63A5;&#x6536;&#x5F53;&#x524D;&#x6B63;&#x5728;&#x89E6;&#x53D1;&#x7684;&#x4E8B;&#x4EF6;&#x3002;&#x5982;&#x679C;&#x53D6;&#x6D88;&#x76D1;&#x542C;&#xFF0C;&#x76D1;&#x542C;&#x8005;&#x4F1A;&#x7ACB;&#x5373;&#x505C;&#x6B62;&#x63A5;&#x6536;&#x4E8B;&#x4EF6;&#x3002;</p>
<p>&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;&#x5E7F;&#x64AD;&#x6D41;&#xFF0C;&#x4E00;&#x79CD;&#x76F4;&#x63A5;&#x4ECE;<code>Stream</code>&#x521B;&#x5EFA;&#xFF0C;&#x53E6;&#x4E00;&#x79CD;&#x4F7F;&#x7528;<code>StreamController</code>&#x521B;&#x5EFA;</p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  <span class="hljs-comment">// &#x8C03;&#x7528; Stream &#x7684; asBroadcastStream &#x65B9;&#x6CD5;&#x521B;&#x5EFA;</span>
  Stream&lt;<span class="hljs-built_in">int</span>&gt; stream = Stream&lt;<span class="hljs-built_in">int</span>&gt;.periodic(<span class="hljs-built_in">Duration</span>(seconds: <span class="hljs-number">1</span>), (e)=&gt;e)
  .asBroadcastStream();
  stream = stream.take(<span class="hljs-number">5</span>);

  stream.listen(<span class="hljs-built_in">print</span>);
  stream.listen(<span class="hljs-built_in">print</span>);
}
</code></pre>
<p>&#x4F7F;&#x7528;<code>StreamController</code></p>
<pre><code class="lang-dart">test() <span class="hljs-keyword">async</span>{
  <span class="hljs-comment">// &#x521B;&#x5EFA;&#x5E7F;&#x64AD;&#x6D41;</span>
  StreamController sc = StreamController.broadcast();

  sc.stream.listen(<span class="hljs-built_in">print</span>);
  sc.stream.listen(<span class="hljs-built_in">print</span>);

  sc.add(<span class="hljs-string">&quot;event1&quot;</span>);
  sc.add(<span class="hljs-string">&quot;event2&quot;</span>);
}
</code></pre>
<h2 id="streamtransformer"><a name="streamtransformer" class="anchor-navigation-ex-anchor" href="#streamtransformer"><i class="fa fa-link" aria-hidden="true"></i></a>StreamTransformer</h2>
<p>&#x8BE5;&#x7C7B;&#x53EF;&#x4EE5;&#x4F7F;&#x6211;&#x4EEC;&#x5728;<code>Stream</code>&#x4E0A;&#x6267;&#x884C;&#x6570;&#x636E;&#x8F6C;&#x6362;&#x3002;&#x7136;&#x540E;&#xFF0C;&#x8FD9;&#x4E9B;&#x8F6C;&#x6362;&#x88AB;&#x63A8;&#x56DE;&#x5230;&#x6D41;&#x4E2D;&#xFF0C;&#x4EE5;&#x4FBF;&#x8BE5;&#x6D41;&#x6CE8;&#x518C;&#x7684;&#x6240;&#x6709;&#x76D1;&#x542C;&#x5668;&#x53EF;&#x4EE5;&#x63A5;&#x6536;</p>
<p><strong>&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x539F;&#x578B;</strong></p>
<pre><code class="lang-dart"><span class="hljs-keyword">factory</span> StreamTransformer.fromHandlers({
      <span class="hljs-keyword">void</span> handleData(S data, EventSink&lt;T&gt; sink),
      <span class="hljs-keyword">void</span> handleError(<span class="hljs-built_in">Object</span> error, StackTrace stackTrace, EventSink&lt;T&gt; sink),
      <span class="hljs-keyword">void</span> handleDone(EventSink&lt;T&gt; sink)
})
</code></pre>
<ul>
<li><code>handleData</code>&#xFF1A;&#x54CD;&#x5E94;&#x4ECE;&#x6D41;&#x4E2D;&#x53D1;&#x51FA;&#x7684;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x4E8B;&#x4EF6;&#x3002;&#x63D0;&#x4F9B;&#x7684;&#x53C2;&#x6570;&#x662F;&#x6765;&#x81EA;&#x53D1;&#x51FA;&#x4E8B;&#x4EF6;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4EE5;&#x53CA;<code>EventSink&lt;T&gt;</code>&#xFF0C;&#x8868;&#x793A;&#x6B63;&#x5728;&#x8FDB;&#x884C;&#x6B64;&#x8F6C;&#x6362;&#x7684;&#x5F53;&#x524D;&#x6D41;&#x7684;&#x5B9E;&#x4F8B;<ul>
<li><code>handleError</code>&#xFF1A;&#x54CD;&#x5E94;&#x4ECE;&#x6D41;&#x4E2D;&#x53D1;&#x51FA;&#x7684;&#x4EFB;&#x4F55;&#x9519;&#x8BEF;&#x4E8B;&#x4EF6;</li>
<li><code>handleDone</code>&#xFF1A;&#x5F53;&#x6D41;&#x4E0D;&#x518D;&#x6709;&#x6570;&#x636E;&#x8981;&#x5904;&#x7406;&#x65F6;&#x8C03;&#x7528;&#x3002;&#x901A;&#x5E38;&#x5728;&#x6D41;&#x7684;<code>close()</code>&#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#x56DE;&#x8C03;</li>
</ul>
</li>
</ul>
<pre><code class="lang-dart"><span class="hljs-keyword">void</span> test() {
  StreamController sc = StreamController&lt;<span class="hljs-built_in">int</span>&gt;();

  <span class="hljs-comment">// &#x521B;&#x5EFA; StreamTransformer&#x5BF9;&#x8C61;</span>
  StreamTransformer stf = StreamTransformer&lt;<span class="hljs-built_in">int</span>, <span class="hljs-built_in">double</span>&gt;.fromHandlers(
    handleData: (<span class="hljs-built_in">int</span> data, EventSink sink) {
      <span class="hljs-comment">// &#x64CD;&#x4F5C;&#x6570;&#x636E;&#x540E;&#xFF0C;&#x8F6C;&#x6362;&#x4E3A; double &#x7C7B;&#x578B;</span>
      sink.add((data * <span class="hljs-number">2</span>).toDouble());
    }, 
    handleError: (error, stacktrace, sink) {
      sink.addError(<span class="hljs-string">&apos;wrong: $error&apos;</span>);
    }, 
    handleDone: (sink) {
      sink.close();
    },
  );

  <span class="hljs-comment">// &#x8C03;&#x7528;&#x6D41;&#x7684;transform&#x65B9;&#x6CD5;&#xFF0C;&#x4F20;&#x5165;&#x8F6C;&#x6362;&#x5BF9;&#x8C61;</span>
  Stream stream = sc.stream.transform(stf);

  stream.listen(<span class="hljs-built_in">print</span>);

  <span class="hljs-comment">// &#x6DFB;&#x52A0;&#x6570;&#x636E;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;&#x7C7B;&#x578B;&#x662F;int</span>
  sc.add(<span class="hljs-number">1</span>);
  sc.add(<span class="hljs-number">2</span>); 
  sc.add(<span class="hljs-number">3</span>); 

  <span class="hljs-comment">// &#x8C03;&#x7528;&#x540E;&#xFF0C;&#x89E6;&#x53D1;handleDone&#x56DE;&#x8C03;</span>
  <span class="hljs-comment">// sc.close();</span>
}
</code></pre>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>2.0
4.0
6.0
</code></pre><h2 id="&#x603B;&#x7ED3;"><a name="&#x603B;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x603B;&#x7ED3;</h2>
<p>&#x4E0E;&#x6D41;&#x76F8;&#x5173;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x4E3B;&#x8981;&#x6709;&#x56DB;&#x4E2A;&#x7C7B;</p>
<ul>
<li><code>Stream</code></li>
<li><code>StreamController</code></li>
<li><code>StreamSink</code></li>
<li><code>StreamSubscription</code></li>
</ul>
<p><code>Stream</code>&#x662F;&#x57FA;&#x7840;&#xFF0C;&#x4E3A;&#x4E86;&#x66F4;&#x65B9;&#x4FBF;&#x63A7;&#x5236;&#x548C;&#x7BA1;&#x7406;<code>Stream</code>&#xFF0C;&#x51FA;&#x73B0;&#x4E86;<code>StreamController</code>&#x7C7B;&#x3002;&#x5728;<code>StreamController</code>&#x7C7B;&#x4E2D;&#xFF0C; &#x63D0;&#x4F9B;&#x4E86;<code>StreamSink</code> &#x4F5C;&#x4E3A;&#x4E8B;&#x4EF6;&#x8F93;&#x5165;&#x53E3;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x8C03;&#x7528;<code>add</code>&#x65F6;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x8C03;&#x7528;&#x7684;<code>sink.add</code>&#xFF0C;&#x901A;&#x8FC7;<code>sink</code>&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;<code>StreamController</code>&#x7C7B;&#x4E2D;&#x7684;<code>StreamSink</code> &#xFF0C;&#x800C;<code>StreamSubscription</code>&#x7C7B;&#x5219;&#x7528;&#x4E8E;&#x7BA1;&#x7406;&#x4E8B;&#x4EF6;&#x7684;&#x6CE8;&#x518C;&#x3001;&#x6682;&#x505C;&#x4E0E;&#x53D6;&#x6D88;&#x7B49;&#xFF0C;&#x901A;&#x8FC7;&#x8C03;&#x7528;<code>stream.listen</code>&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;<code>StreamSubscription</code>&#x5BF9;&#x8C61;&#x3002;</p>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-03-18 13:34:43
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="异步编程之Future.html" class="navigation navigation-prev " aria-label="Previous page: Future">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="异步编程之Isolate.html" class="navigation navigation-next " aria-label="Next page: Isolate">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Stream","level":"1.3.5.2","depth":3,"next":{"title":"Isolate","level":"1.3.5.3","depth":3,"path":"异步编程之Isolate.md","ref":"异步编程之Isolate.md","articles":[]},"previous":{"title":"Future","level":"1.3.5.1","depth":3,"path":"异步编程之Future.md","ref":"异步编程之Future.md","articles":[]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"异步编程之Stream.md","mtime":"2021-03-18T05:34:43.631Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

